<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Uword;
use App\Models\Collect;

class CollectController extends Controller
{
    public function List(Request $request){
        $page = $request->get('page');
        $data = Uword::where("id",'>',$page)->where("user",$request->get('openid'))->where("collect",$request->get('collect'))->limit(10)->get();
        $page = count($data);
        if($page == 0){
            return response()->json([
                "status"=>-1,
                "data"=>$data,
                'page'=>$request->get('page')
            ]);
        }else {
            $page = $data[$page-1]['id'];
            return response()->json([
                "status"=>200,
                "data"=>$data,
                'page'=>$page
            ]);
        }

    }

    //列出收藏夹
    public function Listcollect(Request $request){

        $data = Collect::where("user",$request->get('openid'))->get();



        if($data){
            return response()->json([
                "status"=>200,
                "data"=>$data
            ]);
        }

    }
//删除收藏夹
    public function Delcollect(Request $request){
        $collect = $request->get('collect');
        $user = $request->get('openid');

        $res = Collect::where("collect_name",$collect)->where("user",$user)->get();

        //var_dump($res);

        $data = $res[0]->delete();

        if($data){
            return response()->json([
                "status"=>200,
                "data" => $data
            ]);
        }

    }

    //增加收藏夹

    public function Addcollect(Request $request){

        $collect = $request->get("collect");

        //判断是否重复

        $res = Collect::where("collect_name",$collect)->get();


        if($res->first()){
            return response()->json([
                "status"=>-1
            ]);
        }


        $res = Collect::create(["user"=>$request->get('openid'),"collect_name"=>$request->get('collect')]);
        if($res){
            return response()->json([
                "status"=>200
            ]);
        }
    }

    //收藏夹索引

    public function Indexcollect(Request $request) {
        $res = Collect::where("user",$request->get('openid'))->get();
        $data = [];
        foreach ($res as $val){
            $data[$val['id']]=$val["COLLECT_NAME"];
        }
        $data[0] = "默认收藏本";


        return response()->json([
            "data"=>$data
        ]);

    }
}






























